import { _decorator, Label, ProgressBar } from "cc";
import { BaseView } from "../../base/BaseView";
import { EventId } from "../../com/EventMgr";
import { layerMgr } from "../../com/LayerMgr";
const { ccclass, property } = _decorator;

/**
 * 加载界面
 */
@ccclass("LoadingView")
export class LoadingView extends BaseView {

    @property(Label)
    labLoading: Label;
    @property(ProgressBar)
    progBar: ProgressBar;
    @property(Label)
    labLoad: Label;
    @property(Label)
    labProg: Label;

    loading = 0;

    protected onOpen(): void {
        layerMgr.closeAll(this);
        this.on(EventId.loadingStr, this.onLoadingStr);
        this.on(EventId.loadingProg, this.onLoadingProg);
        this.schedule(this.lodingAnim, 0.3);
    }

    protected offListeners(): void {
        this.unschedule(this.lodingAnim);
    }

    protected onClose(): void {

    }

    lodingAnim() {
        let str = "Loading";
        let dot = "";
        for (let i = 0; i < 5; i++) {
            dot += i < this.loading ? "." : " ";
        }
        if (++this.loading > 5) this.loading = 0;
        this.labLoading.string = str + dot;
    }

    onLoadingStr(data: EventData[EventId.loadingStr]) {
        this.labLoad.string = data;
    }

    onLoadingProg(data: EventData[EventId.loadingProg]) {
        this.progBar.progress = data;
        this.labProg.string = `${data * 100 >> 0}/100`;
    }

}